﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace GameBanChim_Version1
{
    public class Button : Dialog
    {
        bool _isPlaySound = true;

        public bool IsPlaySound
        {
            get { return _isPlaySound; }
            set { _isPlaySound = value; }
        }

        SoundEffect _sound;
        SoundEffectInstance _instance;

        public SoundEffectInstance Instance
        {
            get { return _instance; }
            set { _instance = value; }
        }

        public bool isHover()
        {
            if (Rec.Contains(GlobalVariables.CurMouseState.X, GlobalVariables.CurMouseState.Y))
            {
                Sprite[0].Itexture2D = 1;
                return true;
            }
            return false;
        }
        public bool isClicked()
        {
            if (isHover() && GlobalVariables.PreMouseState.LeftButton == ButtonState.Pressed && GlobalVariables.CurMouseState.LeftButton == ButtonState.Released)
            {
                if (_isPlaySound)
                {
                    _instance.Play();
                }
                return true;
            }
            return false;
        }
        public bool isDown()
        {
            if (isHover() && GlobalVariables.CurMouseState.LeftButton == ButtonState.Pressed)
            {
                // do nothing
                return true;
            }
            return false;
        }
        public bool isRelease()
        {
            if (Rec.Contains(GlobalVariables.PreMouseState.X, GlobalVariables.PreMouseState.Y) && !Rec.Contains(GlobalVariables.CurMouseState.X, GlobalVariables.CurMouseState.Y) )
            {
                Sprite[0].Itexture2D = 0;
                return true;
            }
            return false;
        }
        public override bool Init(ContentManager Content, int index, string strResourceXML)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(strResourceXML);

            XmlNode _button = doc.DocumentElement.SelectSingleNode("//Button[@id='" + index.ToString() + "']");

            XmlNodeList _listOfImage = _button.SelectNodes("Image");
            Texture2D[] _temp = new Texture2D[_listOfImage.Count];
            for (int i = 0; i < _listOfImage.Count; i++)
            {
                _temp[i] = Content.Load<Texture2D>(_listOfImage[i].InnerText);
            }

            Nsprite = 1;
            Sprite = new MySprite[Nsprite];
            Sprite[0] = new MySprite(_temp, 0, 0, 0);
            X = int.Parse(_button.SelectSingleNode("X").InnerText);
            Y = int.Parse(_button.SelectSingleNode("Y").InnerText);
            Rec = new Rectangle(
                (int)X,
                (int)Y,
                int.Parse(_button.SelectSingleNode("Width").InnerText),
                int.Parse(_button.SelectSingleNode("Height").InnerText)
                );
            _sound = Content.Load<SoundEffect>("Sound/Click");
            _instance = _sound.CreateInstance();
            _instance.Volume = 0.5f;

            return true;
        }

        public override VisibleGameObject Clone()
        {
            Button _newButton = new Button();
            _newButton.Nsprite = Nsprite;
            _newButton.Sprite = new MySprite[_newButton.Nsprite];
            for (int i = 0; i < _newButton.Nsprite; i++)
            {
                _newButton.Sprite[i] = Sprite[i];
            }
            _newButton._sound = _sound;
            _newButton._instance = _instance;
            _newButton.X = X;
            _newButton.Y = Y;
            _newButton.Rec = Rec;
            return _newButton;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
        }
    }
}
